﻿using RimWorld;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Verse;
namespace Miao.ArchotechFix
{
    public class HediffCompProperties_AutoRecovery : HediffCompProperties
    {
        public int tickMultiflier = 1800;
        public float healPoint = 1;
        public List<HediffDef> healHediffDefs = new List<HediffDef>();

        public HediffCompProperties_AutoRecovery()
        {
            this.compClass = typeof(HediffComp_AutoRecovery);
        }
    }


    public class HediffComp_AutoRecovery : HediffComp//大武器JecsTools前置
    {
        private int ticksToHeal;
        private float healPoint;

        public override string CompTipStringExtra => $"{"再生".Translate()}{Props.tickMultiflier / 60}{"点耐久".Translate()}/{healPoint}{"秒".Translate()}";
        public HediffCompProperties_AutoRecovery Props
        {
            get
            {
                return (HediffCompProperties_AutoRecovery)props;
            }
        }

        public override void CompPostMake()
        {
            base.CompPostMake();
            ResetTicksToHeal();
        }
        public override void CompPostTick(ref float severityAdjustment)
        {
            ticksToHeal--;
            if (ticksToHeal <= 0)
            {
                TryHealRandomPermanentWound();
                TryHealRandomHediffTendable();
                List<Hediff> hediffs = Pawn.health.hediffSet.hediffs;
                for (int i = 0; i < hediffs.Count; i++)
                {
                    for (int k = 0; k < Props.healHediffDefs.Count; k++)
                    {
                        if (hediffs[i].def == Props.healHediffDefs[k])
                        {
                            if (this.Pawn.health.hediffSet.HasHediff(hediffs[i].def, false))
                            {
                                this.Pawn.health.hediffSet.GetFirstHediffOfDef(hediffs[i].def, false).Heal(10f);
                            }
                        }
                    }
                }
                ResetTicksToHeal();
            }
        }

        private void ResetTicksToHeal()
        {
            healPoint = Props.healPoint;
            ticksToHeal = Props.tickMultiflier;
        }
        /// <summary>
        /// 尝试治疗随机永久性伤口
        /// </summary>
        private void TryHealRandomPermanentWound()
        {
            IEnumerable<Hediff> hediffs = Pawn.health.hediffSet.hediffs;
            bool recold = false;
            foreach (Hediff hediff in hediffs)
            {
                if (hediff.def.isBad && hediff.IsPermanent())
                {
                    recold = true;
                    hediff.Severity -= healPoint;
                }
            }
            if(!recold)
            {
               var t =  Pawn.health.hediffSet.GetFirstHediffOfDef(RimWorld.HediffDefOf.BloodLoss);
                if (t != null)
                {
                    t.Severity -= healPoint * 0.01f;
                }
            }
        }
        /// <summary>
        /// 治疗抚慰？
        /// </summary>
        private void TryHealRandomHediffTendable()
        {
            IEnumerable<Hediff> hediffs = Pawn.health.hediffSet.hediffs;
            foreach (Hediff hediff in hediffs)
            {
                if (hediff.def.defName != "Scaria" && hediff.def.isBad && (hediff.IsPermanent() || hediff.def.chronic || hediff.def.tendable || hediff.def.makesSickThought))
                {
                    hediff.Heal(healPoint);
                    if (hediff.Severity <= 0.003)
                    {
                        HealthUtility.Cure(hediff);
                        return;
                    }

                }

            }
        }
    }
}
